package com.it.tydic.estate.module.controller.common;


import com.it.tydic.estate.common.aop.CodeMsg;
import com.it.tydic.estate.common.aop.Result;
import com.it.tydic.estate.module.model.inner.TbRole;
import com.it.tydic.estate.module.service.ResourceService;
import com.it.tydic.estate.module.service.RoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import tk.mybatis.mapper.util.StringUtil;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("role")
@Api(tags = "角色管理接口")
public class RoleController {

    @Autowired
    private RoleService roleService;

    @Autowired
    private ResourceService resourceService;
    @PostMapping("/findAllByPage")
    @ApiOperation(value = "分页及条件查询", notes = "角色列表")
    public Result findAllByPage(@RequestBody Map<String,String> param) {
        if(param == null || param.size() == 0){
            return   Result.error(CodeMsg.EMPTY_PARAM_ERROR);
        }
        return  Result.success(roleService.findAllByPage(param));
    }


    @GetMapping("/findById/{id}")
    @ApiOperation(value = "根据id查询用", notes = "角色信息")
    @ApiImplicitParam(name = "id", value = "用户id",  paramType = "path", required = true, dataType =  "String")
    public Result findById(@PathVariable String id) { //@PathVariable 获取/get/{id}中id
        if(StringUtil.isEmpty(id)){
            Result.error(CodeMsg.EMPTY_PARAM_ERROR);
        }
        return Result.success( roleService.findById(id));
    }


    @DeleteMapping("/deleteById/{id}")
    @ApiOperation(value = "根据id删除数据", notes = "角色信息")
    @ApiImplicitParam(name = "id", value = "用户id",  paramType = "path", required = true, dataType =  "String")
    public Result deleteById(@PathVariable String id) { //@PathVariable 获取/get/{id}中id
        if(StringUtil.isEmpty(id)){
            Result.error(CodeMsg.EMPTY_PARAM_ERROR);
        }
        boolean flag=roleService.roleIsUseById(id);
        if(!flag){
            return Result.success(roleService.deleteById(id));
        }else{
            return Result.error(CodeMsg.ROLE_USE);
        }
    }

    /**
     * 批量删除
     *
     * @param idList 主键集合
     * @return
     */
    @PostMapping("/deleteByIds")
    @ApiOperation(value = "批量删除", notes = "批量删除")
    public Result deleteByIds(@RequestBody List<String> idList) {
        //参数验证
        if(idList == null || idList.size() == 0){
            return   Result.error(CodeMsg.EMPTY_PARAM_ERROR);
        }
        boolean flag=roleService.roleIsUseByList(idList);
        if(!flag){
            int count=roleService.deleteByIds(idList);
            if(count == idList.size()){
                return Result.success();
            }
        }else{
            return Result.error(CodeMsg.ROLE_USE);
        }
        return Result.error(CodeMsg.DELETE_ERROR);
    }

    /**
     * 新增或修改
     *
     * @param tbRole 角色
     * @return
     */
    @PostMapping("/addOrUpdate")
    @ApiOperation(value = "保存或修改", notes = "角色信息")
    public Result addOrUpdate(@RequestBody TbRole tbRole) {
        //参数验证
        if(tbRole == null){
            return   Result.error(CodeMsg.EMPTY_PARAM_ERROR);
        }
        int count=roleService.addOrUpdate(tbRole);
        if(count == 1){
            return Result.success();
        }else if(count == -1){
            return Result.error(CodeMsg.DATA_REPEATE);
        }
        return Result.error(CodeMsg.INTER_ERROR);
    }

    /**
     * 给角色配置菜单
     * @param variables 角色和资源菜单
     *                  resourceIds:用户ID集合
     *                  roleId:角色ID
     * @return
     */
    @PostMapping("/updateRole2Resource")
    @ApiOperation(value = "给角色配置菜单", notes = "角色和资源菜单")
    public Result updateRole2Resource(@RequestBody Map<String,Object> variables) {
        //参数验证
        if(variables == null || variables.size() == 0){
            return   Result.error(CodeMsg.EMPTY_PARAM_ERROR);
        }
        if(variables.get("resourceIds") != null && variables.get("roleId") != null){
            List<String> resourceIds=(List<String>)variables.get("resourceIds");
            String roleId=(String)variables.get("roleId");
            if(resourceIds.size() > 0 && StringUtil.isNotEmpty(roleId)){
                int count=roleService.updateRole2Resource(resourceIds,roleId);
                if(count > 0){
                    return Result.success();
                }
            }
        }
        return Result.error(CodeMsg.INTER_ERROR);
    }


    @GetMapping("/findResourceByRoleId/{roleId}")
    @ApiOperation(value = "根据角色Id菜单", notes = "角色和资源菜单")
    @ApiImplicitParam(name = "roleId", value = "角色id",  paramType = "path", required = true, dataType =  "String")
    public Result findResourceByRoleId(@PathVariable String roleId) { //@PathVariable 获取/get/{id}中id
        if(StringUtil.isEmpty(roleId)){
            Result.error(CodeMsg.EMPTY_PARAM_ERROR);
        }
        return Result.success(resourceService.findResourceTreeListByRoleId(roleId));
    }
    @GetMapping("/findRoleList")
    @ApiOperation(value = "查询角色列表", notes = "角色信息")
    public Result findRoleList() { //@PathVariable 获取/get/{id}中id
        return Result.success(roleService.findRoleList());
    }

}
